package com.mdd.common.plugin.notice.template.mnp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mdd.common.exception.OperateException;
import com.mdd.common.util.StringUtils;
import cn.hutool.http.HttpUtil;
import me.chanjar.weixin.common.error.WxErrorException;

public class WeiXinApplication {

	
	/**
	 * 发货信息录入接口-模拟入参
	 */
	public static void getWxDeliverGoodsVo(String transactionId,Integer logisticsType,String itemDesc,String trackingNo,String expressCompany
			,String openid,String phone){
		WxDeliverGoodsDTO vo = new WxDeliverGoodsDTO();
		OrderKeyDTO order_key = new OrderKeyDTO();
		order_key.setMchid("");
		order_key.setOrder_number_type(2);	// 必填
		order_key.setOut_trade_no("");
		order_key.setTransaction_id(transactionId);

		ArrayList<ShippingDTO> list = new ArrayList<>();
		ShippingDTO shipping = new ShippingDTO();
		shipping.setItem_desc(itemDesc);	// 必填
		
		shipping.setExpress_company(expressCompany);
		shipping.setTracking_no(trackingNo);
		if(StringUtils.isNotEmpty(phone)) {
			ContactDTO contact = new ContactDTO();
			contact.setConsignor_contact("");	// 手机号掩码处理
			contact.setReceiver_contact(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
			shipping.setContact(contact);
		}
		list.add(shipping);

		PayerDTO payer = new PayerDTO();
		payer.setOpenid(openid);	// 必填

		vo.setOrder_key(order_key);	// 必填
		vo.setLogistics_type(logisticsType);	// 必填
		vo.setDelivery_mode(1);		// 必填
		vo.setIs_all_delivered(false);	// 分拆发货模式时必填
		vo.setShipping_list(list);	// 必填
//		vo.setUpload_time("2022-12-15T13:29:35.120+08:00");	// 必填
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
		vo.setUpload_time(sdf.format(new Date()));	// 必填
		vo.setPayer(payer);	// 必填
		execute(vo);
	}
	
	public static void execute(WxDeliverGoodsDTO vo) {
		String url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token="+getAccessToken()+"";
		String param = JSONObject.toJSONString(vo);
		String result = HttpUtil.post(url, param);
		JSONObject jsonObject = JSON.parseObject(result);
		if(0 != jsonObject.getIntValue("errcode")) {
			 throw new OperateException(jsonObject.getString("errmsg"));
		}
	}
	
	public static String getAccessToken() {
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx03d3a4501c174a80&secret=b5ba2c574608a0fa9a3d3a2f31202a13";
		String result = HttpUtil.get(url);
		JSONObject jsonObject = JSON.parseObject(result);
		return jsonObject.getString("access_token");
	}
	
	public static void main(String[] args) throws WxErrorException {

//		String accessToken = "86_KKa1FknJ_u5D1Oyb4QZaGQXPspVyAFf1WxIVY28W9aWDbjq-KsWDd4TJc0DAr79evK_OuQYPOJTjcaxk6b2pNjo3_P-9jvOgL05XVrDZIhhHhbX8uBZ45HK11M8UWOjAFAHJB";
//		/* 获取模拟入参 */
//		
//		getDeliveryList(accessToken);
		
		//WxDeliverGoodsDTO vo = getWxDeliverGoodsVo("4200002559202411209921835733", 1, "电器发货", "YT202411201015", "YTO", "oCtCX69ezheabwXTS9AxUnWqAXKo", "17347502680");
//		String url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token="+accessToken+"";
//		String param = JSONObject.toJSONString(vo);
//		System.out.println("入参 = " + param);
//		String result = HttpUtil.post(url, param);
//		JSONObject jsonObject = JSON.parseObject(result);
//		System.out.println(jsonObject);
		System.out.println(getAccessToken());
	}
	
	
	
	public static void getDeliveryList(String accessToken) {
		String url = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/get_delivery_list?access_token="+accessToken+"";
		// 无参post请求
		String result = HttpUtil.post(url, String.valueOf(new HashMap<String,Object>()));

		JSONObject jsonObject = JSON.parseObject(result);
		int errcode = jsonObject.getInteger("errcode");
		if (errcode != 0) {
			String errmsg = jsonObject.getString("errmsg");
			String err = String.format("微信小程序-获取运力id列表get_delivery_list接口异常，code码：%s, msg：%s", errcode, errmsg);
			System.out.println(err);
		}
		com.alibaba.fastjson.JSONArray deliveryList = jsonObject.getJSONArray("delivery_list");
		List<JSONObject> list = deliveryList.toJavaList(JSONObject.class);
		for (JSONObject o : list) {
//			String deliveryId = o.getString("delivery_id");
//			String deliveryName = o.getString("delivery_name");
			System.out.println("o = " + o);
		}
		System.out.println("jsonObject = " + jsonObject);
	}
}
